<template>
  <div class="personal-page-container">
    <!-- 个人信息卡片 -->
    <div class="user-info-card">
      <div class="dot"></div>
      <div class="dot dot-small"></div>
      <img
        :src="
          user.avatar
            ? `data:image/jpeg;base64,${user.avatar}`
            : 'https://picsum.photos/200/200?random=4'
        "
        alt="用户头像"
        class="user-avatar"
      />
      <h3 class="user-name">{{ user.nickname }}</h3>
      <p class="user-intro">{{ userInfo.personalIntroduction }}</p>
      <p class="user-blog-intro">{{ userInfo.blogIntroduction }}</p>
      <p class="user-identity">{{ userInfo.identityInfo }}</p>
      <div class="user-stats">
        <span><i class="el-icon-document"></i> 文章：{{ userInfo.articleCount }}</span>
        <span><i class="el-icon-view"></i> 总访问量：{{ userInfo.totalViews }}</span>
        <span @click="goToFollowingPage"><i class="el-icon-user"></i> 关注：{{ userInfo.followingCount }}</span>
        <span @click="goToFollowerPage"><i class="el-icon-star-off"></i> 粉丝：{{ userInfo.followerCount }}</span>
      </div>
      <!-- 如果是登录用户的个人页面，显示修改信息按钮 -->
      <div v-if="isCurrentUser">
        <el-button @click="goToUpdateUserInfo">修改信息</el-button>
      </div>
    </div>

    <!-- 已发布文章区域 -->
    <div class="article-section">
      <h2 class="section-title">已发布文章</h2>
      <div class="articles-container">
        <div
          class="article-card"
          v-for="article in publishedArticles"
          :key="article.id"
          @mouseenter="scaleCard(article.id)"
          @mouseleave="resetScale(article.id)"
          :style="{ '--scale': cardScale[article.id] || 1 }"
          @click="goToArticleDetail(article.id)"
          style="cursor: pointer;"
        >
          <div class="article-image-container">
            <img
              :src="
                article.coverImage
                  ? `data:image/jpeg;base64,${article.coverImage}`
                  : 'https://picsum.photos/400/200'
              "
              alt="文章封面"
              class="article-image"
            />
            <div class="article-badge" v-if="article.isNew">NEW</div>
          </div>
          <div class="article-content">
            <h3 class="article-title">{{ article.title }}</h3>
            <p class="article-introduction">{{ article.introduction }}</p>
            <div class="article-meta">
              <span class="article-author">
                <i class="el-icon-user"></i> {{ user.nickname }}
              </span>
              <span class="article-date">
                <i class="el-icon-date"></i>
                {{ formatDate(article.publishTime) }}
              </span>
            </div>
            <div class="article-tags">
              <span
                class="tag"
                v-for="tag in article.tag.split(',')"
                :key="tag"
              >{{ tag }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 草稿箱文章区域 -->
    <div class="article-section">
      <h2 class="section-title">草稿箱文章</h2>
      <div class="articles-container">
        <div
          class="article-card"
          v-for="article in draftArticles"
          :key="article.id"
          @mouseenter="scaleCard(article.id)"
          @mouseleave="resetScale(article.id)"
          :style="{ '--scale': cardScale[article.id] || 1 }"
          @click="goToEditArticle(article.id)"
          style="cursor: pointer;"
        >
          <div class="article-image-container">
            <img
              :src="
                article.coverImage
                  ? `data:image/jpeg;base64,${article.coverImage}`
                  : 'https://picsum.photos/400/200'
              "
              alt="文章封面"
              class="article-image"
            />
            <div class="article-badge" v-if="article.isNew">NEW</div>
          </div>
          <div class="article-content">
            <h3 class="article-title">{{ article.title }}</h3>
            <p class="article-introduction">{{ article.introduction }}</p>
            <div class="article-meta">
              <span class="article-author">
                <i class="el-icon-user"></i> {{ user.nickname }}
              </span>
              <span class="article-date">
                <i class="el-icon-date"></i>
                {{ formatDate(article.publishTime) }}
              </span>
            </div>
            <div class="article-tags">
              <span
                class="tag"
                v-for="tag in article.tag.split(',')"
                :key="tag"
              >{{ tag }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  name: "PersonalPage",
  data() {
    return {
      user: {},
      userInfo: {},
      publishedArticles: [],
      draftArticles: [],
      cardScale: {},
      isCurrentUser: false
    };
  },
  mounted() {
    this.fetchUserData();
    this.fetchArticles();
    this.checkIsCurrentUser();
  },
  methods: {
    formatDate(timestamp) {
      return new Date(timestamp).toLocaleDateString();
    },
    goToArticleDetail(articleId) {
      this.$router.push(`/viewArticle/${articleId}`);
    },
    goToEditArticle(articleId) {
      this.$router.push(`/writeArticle/${articleId}`);
    },
    goToFollowingPage() {
      const userId = this.$route.params.userId;
      this.$router.push(`/user/${userId}/following`);
    },
    goToFollowerPage() {
      const userId = this.$route.params.userId;
      this.$router.push(`/user/${userId}/followers`);
    },
    goToUpdateUserInfo() {
      this.$router.push("/user/update");
    },
    scaleCard(id) {
      this.$set(this.cardScale, id, 1.03);
    },
    resetScale(id) {
      this.$set(this.cardScale, id, 1);
    },
    async fetchUserData() {
      const userId = this.$route.params.userId;
      try {
        // 获取用户信息
        const userResponse = await this.$axios.get(`/user/info/${userId}`);
        if (userResponse.data.code === 200) {
          this.user = userResponse.data.data;
        }
        // 获取用户详细信息
        const userInfoResponse = await this.$axios.get(`/userInfo/get/${userId}`);
        if (userInfoResponse.data.code === 200) {
          this.userInfo = userInfoResponse.data.data;
        }
      } catch (error) {
        console.error("获取用户数据失败", error);
      }
    },
    async fetchArticles() {
      const userId = this.$route.params.userId;
      try {
        // 获取已发布文章
        const publishedResponse = await this.$axios.get(`/article/list/${userId}`, {
          params: {
            status: '已发布'
          }
        });
        if (publishedResponse.data.code === 200) {
          this.publishedArticles = publishedResponse.data.data;
        }
        // 获取草稿箱文章
        const draftResponse = await this.$axios.get(`/article/list/${userId}`, {
          params: {
            status: '草稿'
          }
        });
        if (draftResponse.data.code === 200) {
          this.draftArticles = draftResponse.data.data;
        }
      } catch (error) {
        console.error("获取文章数据失败", error);
      }
    },
    checkIsCurrentUser() {
      const loggedInUserId = localStorage.getItem('userId');
      const currentUserId = this.$route.params.userId;
      this.isCurrentUser = loggedInUserId === currentUserId;
    }
  }
};
</script>

<style scoped>
.personal-page-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 50px;
  padding: 30px 40px;
  min-height: 100vh;
  background: transparent;
  position: relative;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

.user-info-card {
  text-align: center;
  position: relative;
  padding-bottom: 20px;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(5px);
  border-radius: 20px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
  padding: 25px;
}

.dot {
  position: absolute;
  right: 20px;
  bottom: 20px;
  width: 30px;
  height: 30px;
  background: rgba(255, 183, 107, 0.1);
  border-radius: 50%;
}

.dot-small {
  width: 15px;
  height: 15px;
  right: 40px;
  bottom: 40px;
}

.user-avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: 0 auto 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  border: 3px solid white;
  transition: transform 0.3s;
}

.user-avatar:hover {
  transform: rotate(10deg);
}

.user-name {
  font-size: 20px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.user-intro {
  font-size: 14px;
  color: #606266;
  margin-bottom: 10px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.user-blog-intro {
  font-size: 14px;
  color: #606266;
  margin-bottom: 10px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.user-identity {
  font-size: 14px;
  color: #606266;
  margin-bottom: 15px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.user-stats {
  color: #909399;
  font-size: 14px;
  margin-top: 15px;
  display: flex;
  justify-content: center;
  gap: 20px;
  cursor: pointer;
}

.user-stats i {
  margin-right: 5px;
}

.article-section {
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(5px);
  border-radius: 20px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
  padding: 25px;
}

.section-title {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 30px;
  position: relative;
  padding-left: 15px;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 5px;
  height: 20px;
  width: 5px;
  background: #ffb76b;
  border-radius: 3px;
}

.articles-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
}

.article-card {
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s;
  transform: scale(var(--scale, 1));
  position: relative;
}

.article-card:hover {
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
  transform: scale(var(--scale, 1.03));
}

.article-image-container {
  position: relative;
  overflow: hidden;
  height: 200px;
}

.article-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.article-card:hover .article-image {
  transform: scale(1.05);
}

.article-badge {
  position: absolute;
  top: 15px;
  right: 15px;
  background: #ff6b6b;
  color: white;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.article-content {
  padding: 20px;
}

.article-title {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
  line-height: 1.4;
}

.article-introduction {
  font-size: 14px;
  color: #606266;
  margin-bottom: 15px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-meta {
  font-size: 12px;
  color: #909399;
  margin-bottom: 15px;
}

.article-meta i {
  margin-right: 5px;
}

.article-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.tag {
  background: #f0f0f0;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  color: #606266;
}
</style>